/*
 * Cpyright 2013 Insu Song. All rights reserved.
 * inssong@gmail.com  http://www.kopo.com  
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied.
 */

package com.kopo.droidlipokit;

import com.kopo.droidioiolib.Behavior;
import com.kopo.droidioiolib.ControlInterface;
import com.kopo.droidioiolib.LipoKitInterface;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * To do:
 * 	Download a behavior definition from URL
 *  Use web interface to author behavior
 *  Simulate using JavaScript
 *  Behavior repository
 *   
 * @author insu.song
 *
 */
public class BehaviorGeneral extends Behavior {
	private TextView bv_txt;
	private ToggleButton bv_button;
	private Spinner bv_spinner;

	private String currentMsg = "";
	private static final String TAG = "kopo.log"; 
	private boolean isReset = false;
	
	public BehaviorGeneral(View view) {
		super(view);
		Log.d(TAG, "BehaviorSensor created");
	}

	@Override
	public void setup() {
		Log.d(TAG, "BehaviorGeneral.setup");

		// Init View
		bv_txt = (TextView) view.findViewById(R.id.bv_txt);
		bv_button = (ToggleButton) view.findViewById(R.id.bv_button);
		bv_spinner = (Spinner) view.findViewById(R.id.bv_spinner);
		// Init AI

		this.initialized = true;
		isReset = false;
	}

	@Override
	public void loop() {
		if( !initialized )
		{
			this.setup();
		}

		//--- AI
		int selectedId = bv_spinner. getSelectedItemPosition ();
		if( selectedId == Spinner.INVALID_POSITION || !bv_button.isChecked() )
		{
			resetDevice();
		}
		else
		{
			isReset = false;
			switch( selectedId )
			{
				case 0:
					// Behavior 1
					bug1Behavior();
					break;
				case 1:
					// Behavior 2
					resetDevice();
					break;
				default:
					// No behavior
					resetDevice();
					break;
			}
		}

		//--- Update UI if any
		if( MyControlDevice.currentActivity != null)
			MyControlDevice.currentActivity.runUI(); // invoke uiUpdate in UIThread		
	}

	@Override
	public void cleanup() {
		// TODO Auto-generated method stub
	}

	@Override
	public void uiUpdate(Context context) {
		bv_txt.setText(currentMsg);
	}

	/**
	 * Simple bug behavior.
	 * Run and stop if there is an obstacle in front
	 * turn until there is no obstacle
	 */
	//boolean bug1Behavior_forward = true;
	//int bug1Behavior_loopNo = 0;
	//boolean bug1Behavior_isInitialized = false;
	
	public void bug1Behavior()
	{
		//Log.d(TAG, "BehaviorGeneral.bug1Behavior");
		LipoKitInterface i = (LipoKitInterface) ControlInterface.me;

		float v = i.readIR1();
		currentMsg = "IRSensor1 = " + v;

		if( v > 0 )
		{
			// move forward
			currentMsg = currentMsg + " moving forward.";
			i.setPwm(1, 50, 1);
			
			// Stop turning
			i.setPwm(0, 0, 2);
		}
		else
		{
			currentMsg = currentMsg + " turning.";
			// Stop moving forward
			i.setPwm(1, 0, 2);
			
			// turning
			i.setPwm(0, 50, 1);
		}
	}

	private void resetDevice()
	{
		if( !isReset )
		{
			Log.d(TAG, "BehaviorGeneral.resetDevice");
			// Turn off all motors
			ControlInterface.me.setPwm(0, 0, 2);
			ControlInterface.me.setPwm(1, 0, 2);
			currentMsg = "idling";
			isReset = true;
		}
	}
}
